<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
// import storage from "./storage";
import { mapActions } from "vuex";
export default {
  //解决路由跳转页面不刷新获取不到购物车数量的问题
  watch: {
    $route(to, from) {
      this.getCartCount();
    },
  },
  mounted() {
    this.getUser();
    this.getCartCount();
  },
  methods: {
    getUser() {
      //如果服务端跨域要获取cookie，一定要设置
      this.axios.defaults.withCredentials = true;
      this.axios.get("/user").then((res) => {
        //this.$store.dispatch("saveUserName", res[0].username);
        //等价于
        this.saveUserName(res[0].username);
      });
    },
    getCartCount() {
      this.axios
        .get("/carts/prosum")
        .then((res) => {
          //this.$store.dispatch("getCartNum", res[0].num);
          this.getCartNum(res[0].num);
        });
    },
    ...mapActions(["saveUserName", "getCartNum"]), //和22行对应
  },
};
</script>

<style lang="scss">
@import "./assets/scss/reset.scss";
</style>